define(function (require, exports, module) {
    'use strict';

    function ctrl($scope,$stateParams,$filter,$timeout,InfoService,WebService,CommonService,$ionicLoading) {

        var par = $scope.param = new Array();

        $scope.$on('$ionicView.beforeEnter', function() {
            $scope.info = {};
            par.prod_code = $stateParams['prod_code'];
            $scope.today = new Date().getTime();
            init();
        });

        $scope.doRefresh = function() {
            init().finally(function(){
                $scope.$broadcast('scroll.refreshComplete');
            });
        };

        function init(){
            if(!$scope.fund){
                $ionicLoading.show();
            }
            //获取cms基金详情
            return InfoService.getOTCGeneral(par.prod_code).then(function(data){
                if(data){
                    $scope.fund = data;
                    $scope.fund.prod_min_share = $filter('prodMinSubscribe')($scope.fund.prod_min_share);
                    $scope.fund.sellStatus = CommonService.getOtcSellStatus($scope.fund.prod_status, $scope.fund.otc_status);
                    if($scope.fund.sellStatus == 1 || $scope.fund.sellStatus == 2){
                        querySellInfo($scope.fund.prod_code);
                    }
                }
                $ionicLoading.hide();
            });
        }

        function querySellInfo(prod_code){
            WebService.querySellInfo(prod_code).then(
                function (data){
                    if(data && data.length > 0){
                        $scope.info = data[0];
                        //定时刷新
                        if("1" ==  $scope.fund.sellStatus){
                            $timeout(function(){
                                querySellInfo(prod_code);
                            },30000);
                        }
                        //已售罄
                        if("1" == $scope.info.soldout_flag){
                            $scope.fund.sellStatus = 3; // 已售罄
                        }
                    }
                }
            );
        }
    }

    ctrl.$inject = ['$scope','$stateParams','$filter','$timeout','InfoService', 'WebService','CommonService','$ionicLoading'];
    return ctrl;
});